/**
 * 数据源字段相关的API服务
 */

import { request } from '@umijs/max';

// 字段类型枚举
export enum FieldType {
  TEXT = 'text',
  NUMBER = 'number',
  EMAIL = 'email',
  DATE = 'date',
  DATETIME = 'datetime',
  BOOLEAN = 'boolean',
  JSON = 'json',
  LONGTEXT = 'longtext',
  URL = 'url',
  PHONE = 'phone',
  CURRENCY = 'currency',
}

// 数据源字段接口定义
export interface DataSourceField {
  id: number;
  data_source_id: number;
  name: string;
  display_name?: string;
  field_type: FieldType;
  description?: string;
  is_primary: boolean;
  is_nullable: boolean;
  is_email: boolean;
  is_name: boolean;
  sample_value?: string;
  data_format?: string;
  created_at: string;
  updated_at: string;
}

export interface DataSourceFieldUpdate {
  display_name?: string;
  description?: string;
  is_email?: boolean;
  is_name?: boolean;
  sample_value?: string;
  data_format?: string;
}

export interface DataSourceFieldAnalysis {
  fields: DataSourceField[];
  total_fields: number;
  email_fields: string[];
  name_fields: string[];
  primary_fields: string[];
  sample_data?: Record<string, any>;
}

export interface DataSourceDataPreview {
  fields: DataSourceField[];
  data: Record<string, any>[];
  total_rows: number;
  sample_size: number;
}

export interface DataSourceQuery {
  data_source_id: number;
  table_name?: string;
  fields?: string[];
  filters?: Record<string, any>;
  limit?: number;
  offset?: number;
  order_by?: string;
}

export interface FieldMapping {
  source_field: string;
  target_field: string;
  transform_rule?: string;
}

// API服务函数
export async function analyzeDataSourceFields(dataSourceId: number): Promise<DataSourceFieldAnalysis> {
  return request(`/api/data-sources/${dataSourceId}/analyze-fields`, {
    method: 'POST',
  });
}

export async function getDataSourceFields(dataSourceId: number): Promise<DataSourceField[]> {
  return request(`/api/data-sources/${dataSourceId}/fields`, {
    method: 'GET',
  });
}

export async function updateDataSourceField(
  fieldId: number,
  data: DataSourceFieldUpdate
): Promise<DataSourceField> {
  return request(`/api/data-sources/fields/${fieldId}`, {
    method: 'PUT',
    data,
  });
}

export async function queryDataSourceData(
  dataSourceId: number,
  query: Omit<DataSourceQuery, 'data_source_id'>
): Promise<DataSourceDataPreview> {
  return request(`/api/data-sources/${dataSourceId}/query`, {
    method: 'POST',
    data: query,
  });
}

export async function previewDataSourceData(
  dataSourceId: number,
  limit: number = 10
): Promise<DataSourceDataPreview> {
  return request(`/api/data-sources/${dataSourceId}/preview`, {
    method: 'GET',
    params: { limit },
  });
}

// 字段类型映射
export const FIELD_TYPE_MAP = {
  [FieldType.TEXT]: '文本',
  [FieldType.NUMBER]: '数字',
  [FieldType.EMAIL]: '邮箱',
  [FieldType.DATE]: '日期',
  [FieldType.DATETIME]: '日期时间',
  [FieldType.BOOLEAN]: '布尔值',
  [FieldType.JSON]: 'JSON',
  [FieldType.LONGTEXT]: '长文本',
  [FieldType.URL]: '链接',
  [FieldType.PHONE]: '电话',
  [FieldType.CURRENCY]: '货币',
};

// 获取字段类型显示名称
export function getFieldTypeLabel(fieldType: FieldType): string {
  return FIELD_TYPE_MAP[fieldType] || fieldType;
}
